package com.project.frba.bifront.client.view.users;

import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Composite;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.data.FieldDef;
import com.gwtext.client.data.RecordDef;
import com.gwtext.client.data.Store;
import com.gwtext.client.data.StringFieldDef;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.Window;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.gwtext.client.widgets.form.ComboBox;
import com.gwtext.client.widgets.form.FormPanel;
import com.gwtext.client.widgets.form.TextField;
import com.gwtext.client.widgets.layout.FitLayout;
import com.project.frba.bifront.client.model.User;
import com.project.frba.bifront.client.rpc.UserServices;
import com.project.frba.bifront.client.rpc.UserServicesAsync;
import com.project.frba.bifront.client.view.MessageWindow;

public class UserForm extends Composite{

	private Window mainWindow = new Window();
	
	private User user = null;
	
	private Button btSave = new Button("Guardar");
	
	private Button btCancel = new Button("Cancelar");
	
	private TextField tfUsername;
	
	private TextField tfPassword;
	
	private ComboBox cbPrivileges;
	
	private UserList userList = null;
	
	public UserForm(Long id, UserList userList){
		this.userList = userList;
		if(id != 0){
			AsyncCallback<User> callback = new AsyncCallback<User>() {
				public void onFailure(Throwable caught) {
					new MessageWindow("No se ha podido obtener el usuario.");
				}

				public void onSuccess(User userResult) {
					user = userResult;
					loadActions();
					loadForm();
				}
			};
			
			UserServicesAsync userServices = (UserServicesAsync)GWT.create(UserServices.class);
			userServices.getUserById(id, callback);
		} else {
			user = new User();
			//id is 0
			user.setId(id);
			user.setUsername("");
			user.setPassword("");
			user.setPrivilege(null);
			loadActions();
			loadForm();
		}
		
	}
	
	private void loadActions() {
		btSave.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				storeUser();
			}
		});
		
		btCancel.addListener(new ButtonListenerAdapter(){
			public void onClick(Button button, EventObject e) {
				mainWindow.close();
			}
		});
		
	}
	
	private void storeUser() {
		final User voobj = new User();
		voobj.setId(user.getId());
		voobj.setUsername(tfUsername.getValueAsString());
		voobj.setPassword(tfPassword.getValueAsString());
		voobj.setPrivilege(cbPrivileges.getValueAsString());
		
		AsyncCallback<Long> callback = new AsyncCallback<Long>(){
			public void onFailure(Throwable caught) {
				new MessageWindow("No se pudo guardar el usuario, fallo el servicio.");
			}
			
			public void onSuccess(Long result) {
				if(result > 0){
					mainWindow.close();
					voobj.setId(result);
					userList.refreshList();
				} else {
					new MessageWindow("No se guardo el usuario");
				}
			}
		};
		
		UserServicesAsync userServices = (UserServicesAsync)GWT.create(UserServices.class);
		userServices.storeUser(voobj, callback);
		
	}

	public void loadForm(){
		mainWindow.setTitle("Usuario");
		mainWindow.setWidth(400);
		mainWindow.setHeight(170);
		mainWindow.setModal(true);
		mainWindow.setResizable(false);
		mainWindow.setLayout(new FitLayout());
		
		FormPanel formPanel = new FormPanel();
		formPanel.setPaddings(4);
		tfUsername = new TextField("Nombre de Usuario", "username", 250, user.getUsername());
		tfPassword = new TextField("Contrasenia", "password", 250, user.getPassword());
		
		formPanel.add(tfUsername);
		formPanel.add(tfPassword);
		
		cbPrivileges = new ComboBox();
		FieldDef[] fieldDef = new FieldDef[] { 
				new StringFieldDef("id"),
				new StringFieldDef("privilege")};
		RecordDef recordDef = new RecordDef(fieldDef);
		Store store = new Store(recordDef);
		store.add(recordDef.createRecord(new Object[]{"1", "admin"}));
		store.add(recordDef.createRecord(new Object[]{"2", "general"}));
		store.add(recordDef.createRecord(new Object[]{"3", "restringido"}));
		store.commitChanges();
		
		
		cbPrivileges.setStore(store);
		cbPrivileges.setValue(user.getPrivilege());
		
		cbPrivileges.setDisplayField("privilege");
		cbPrivileges.setMode(ComboBox.LOCAL);
		cbPrivileges.setTriggerAction(ComboBox.ALL); 
		cbPrivileges.setWidth("250px");
		cbPrivileges.setLabel("Privilegios: ");
		
		formPanel.add(cbPrivileges);

		mainWindow.add(formPanel);
		mainWindow.setButtons(new Button[]{btSave, btCancel});
		initWidget(mainWindow);	
		mainWindow.show();
	}
}
